#pragma once
#include <iostream>
#include<cstring>
#include"log.hpp"
#include"init.hpp"

init in_it;

std::string opers = "+-*/%";


extern Log lg;


enum
{
    DivZero = 1,
    ModZero,
    UnKnow
};

class task
{
public:
    task()
    {
    }
    task(int sockfd, const std::string &clientip, const uint16_t &clientport)
        : sockfd_(sockfd), clientip_(clientip), clientport_(clientport)
    {
    }

    ~task() {}

    void operator()()
    {
        run();
    }

private:
    void run()
    {
        char buffer[SIZE];
        ssize_t n = read(sockfd_, buffer, SIZE - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << "client say# " << buffer << std::endl;
            std::string echo_string = in_it.translation(buffer);
            n = write(sockfd_, echo_string.c_str(), sizeof(echo_string));
            if(n < 0)
            {
                lg(Warning,"write error...,errno: %d,errstr: %s",errno,strerror(errno));
            }
        }
        else if (n == 0)
        {
            lg(Warning, "%s:%d quit, service sockfd: %d", clientip_.c_str(), clientport_, sockfd_);
        }
        else
        {
            lg(Warning, "read error, sockfd: %d", sockfd_);
        }

        close(sockfd_);
    }

private:
    int sockfd_;
    std::string clientip_;
    uint16_t clientport_;
};